import { defineStore } from 'pinia'
import * as postApi from '../../../api/system/post'
import type { Post } from '../../../types/app.d'

// 岗位管理 Store 接口
interface PostManagementState {
  postList: Post[]
  total: number
  allPosts: Post[]
  loading: boolean
  currentPost: Post | null
}

export const usePostStore = defineStore('postManagement', {
  state: (): PostManagementState => ({
    postList: [],
    total: 0,
    allPosts: [],
    loading: false,
    currentPost: null
  }),

  getters: {
    // 获取岗位列表
    getPostList(): Post[] {
      return this.postList
    },

    // 获取岗位总数
    getPostTotal(): number {
      return this.total
    },

    // 获取所有岗位
    getAllPosts(): Post[] {
      return this.allPosts
    },

    // 获取加载状态
    getLoading(): boolean {
      return this.loading
    },

    // 获取当前岗位
    getCurrentPost(): Post | null {
      return this.currentPost
    }
  },

  actions: {
    // 获取岗位列表
    async getPostListAction(params: {
      page: number
      pageSize: number
      name?: string
      code?: string
      status?: number
      beginTime?: string
      endTime?: string
    }): Promise<void> {
      this.loading = true
      try {
        const response = await postApi.getPostList(params)
        if (response.code === 200) {
          this.postList = response.data.list || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        console.error('获取岗位列表失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 获取所有岗位
    async getAllPostsAction(): Promise<void> {
      try {
        const response = await postApi.getAllPosts()
        if (response.code === 200) {
          this.allPosts = response.data || []
        }
      } catch (error) {
        console.error('获取所有岗位失败:', error)
      }
    },

    // 获取岗位详情
    async getPostInfoAction(id: string): Promise<void> {
      try {
        const response = await postApi.getPostInfo(id)
        if (response.code === 200) {
          this.currentPost = response.data
        }
      } catch (error) {
        console.error('获取岗位详情失败:', error)
      }
    },

    // 创建岗位
    async createPostAction(data: Omit<Post, 'id' | 'createTime' | 'updateTime'>): Promise<boolean> {
      try {
        const response = await postApi.createPost(data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('创建岗位失败:', error)
        return false
      }
    },

    // 更新岗位
    async updatePostAction(id: string, data: Partial<Omit<Post, 'id' | 'createTime'>>): Promise<boolean> {
      try {
        const response = await postApi.updatePost(id, data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('更新岗位失败:', error)
        return false
      }
    },

    // 删除岗位
    async deletePostAction(id: string): Promise<boolean> {
      try {
        const response = await postApi.deletePost(id)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('删除岗位失败:', error)
        return false
      }
    },

    // 批量删除岗位
    async batchDeletePostAction(ids: string[]): Promise<boolean> {
      try {
        const response = await postApi.batchDeletePost(ids)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('批量删除岗位失败:', error)
        return false
      }
    },

    // 修改岗位状态
    async changePostStatusAction(id: string, status: number): Promise<boolean> {
      try {
        const response = await postApi.changePostStatus(id, status)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('修改岗位状态失败:', error)
        return false
      }
    },

    // 清空当前岗位
    clearCurrentPost(): void {
      this.currentPost = null
    }
  }
})